package cn.javabb.admin.controller.biz;

import cn.javabb.common.model.ResponseModel;
import cn.javabb.common.model.Tree;
import cn.javabb.core.base.BaseController;
import cn.javabb.core.entity.biz.PostDO;
import cn.javabb.core.service.biz.CatalogService;
import cn.javabb.core.service.biz.PostService;
import cn.javabb.log.action.DeleteAction;
import cn.javabb.log.annotation.Log;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author QINB imqinbao@163.com
 * @CreateDate 2018/11/27/027 15:19
 * @Since V1.0
 */
@Controller
@RequestMapping("/admin/post")
public class PostAdminController extends BaseController {
    @Autowired
    private PostService postService;
    @Autowired
    private CatalogService catalogService;
    /**
     * 列表页面
     * @author QINB
     * @param model
     * @return
     */
    @GetMapping("/list")
    public String toListPage(Model model) {
        List<Tree> catalogTree = catalogService.listTree();
        model.addAttribute("catalogTree", catalogTree);
        return "admin/biz/post/post";
    }
    /**
     * 保存页面
     * @author QINB
     * @param model
     * @return
     */
    @GetMapping("/save")
    public String toSavePage(Model model) {
        return "admin/biz/post/postSave";
    }

    /**
     * 查询列表
     */
    @ResponseBody
    @GetMapping("/")
    public Object list() {
        JSONObject param = new JSONObject();
        param.put("page",getPage());
        param.put("size",getSize());
        param.put("keyword",getParamValue("keyword"));
        String postState = getParamValue("postState");
        String catalogId = getParamValue("catalogId");
        if("top".equals(postState)){
            param.put("postTop",1);
        }else if("support".equals(postState)){
            param.put("postSupport",1);
        }
        if(StringUtils.isNotBlank(catalogId)&&!"0".equals(catalogId)){
            param.put("catalogId",catalogId);
        }
        param.put("postType",getParamValue("postType"));
        Page articlePage = postService.listPostsPage(param);
        return ResponseModel.table(articlePage);
    }

    /**
     * 更新
     * @param article
     * @return
     */
    @ResponseBody
    @PostMapping("/")
    public Object saveOrUpdate(PostDO article){
        int n = 0;
        if(null == article.getPostId()){
            if(null == article.getState()){article.setState(1);}
            n = postService.insertSelective(article);
        }else{
            n = postService.updateSelective(article);
        }
        if(n>0){
            return ResponseModel.Success("保存成功");
        }
        return ResponseModel.Failure("保存失败");

    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @ResponseBody
    @DeleteMapping("/{ids}")
    @Log(name = "文章删除",action = DeleteAction.class)
    public Object delete(@PathVariable String ids){
        if(StringUtils.isBlank(ids)){
            return ResponseModel.Failure("参数错误");
        }
        if (postService.delete(ids)) {
            return ResponseModel.Success("删除成功");
        }
        return ResponseModel.Failure("删除失败");
    }

    /**
     * 更新状态
     * @return
     */
    @ResponseBody
    @PostMapping("/updateState")
    public Object updateState(PostDO article){
//        if(articleService.updateState(postId,state)){
//            return ResponseModel.Success("状态更新成功");
//        }
        if(postService.updateSelective(article)>0){
            return ResponseModel.Success("更新成功");
        }

        return ResponseModel.Failure("更新失败");
    }
}
